package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IBannerSettingService;
import com.mdd.admin.validate.BannerSettingCreateValidate;
import com.mdd.admin.validate.BannerSettingUpdateValidate;
import com.mdd.admin.validate.BannerSettingSearchValidate;
import com.mdd.admin.vo.BannerSettingListedVo;
import com.mdd.admin.vo.BannerSettingDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.BannerSetting;
import com.mdd.common.mapper.BannerSettingMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 平台banner设置实现类
 * @author DEREK
 */
@Service
public class BannerSettingServiceImpl implements IBannerSettingService {
        
    @Resource
    BannerSettingMapper bannerSettingMapper;

    /**
     * 平台banner设置列表
     *
     * @author DEREK
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<BannerSettingListedVo>
     */
    @Override
    public PageResult<BannerSettingListedVo> list(PageValidate pageValidate, BannerSettingSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<BannerSetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        bannerSettingMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:url:str",
            "=:remark:str",
            "=:type:int",
            "=:businessType@business_type:int",
            "=:businessId@business_id:int",
            "=:requestUrl@request_url:str",
        });

        IPage<BannerSetting> iPage = bannerSettingMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<BannerSettingListedVo> list = new LinkedList<>();
        for(BannerSetting item : iPage.getRecords()) {
            BannerSettingListedVo vo = new BannerSettingListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 平台banner设置详情
     *
     * @author DEREK
     * @param id 主键参数
     * @return BannerSetting
     */
    @Override
    public BannerSettingDetailVo detail(Integer id) {
        BannerSetting model = bannerSettingMapper.selectOne(
                new QueryWrapper<BannerSetting>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        BannerSettingDetailVo vo = new BannerSettingDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 平台banner设置新增
     *
     * @author DEREK
     * @param createValidate 参数
     */
    @Override
    public void add(BannerSettingCreateValidate createValidate) {
        BannerSetting model = new BannerSetting();
        model.setUrl(createValidate.getUrl());
        model.setRemark(createValidate.getRemark());
        model.setType(createValidate.getType());
        model.setBusinessType(createValidate.getBusinessType());
        model.setBusinessId(createValidate.getBusinessId());
        model.setRequestUrl(createValidate.getRequestUrl());
        model.setCreateTime(System.currentTimeMillis() / 1000);
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        bannerSettingMapper.insert(model);
    }

    /**
     * 平台banner设置编辑
     *
     * @author DEREK
     * @param updateValidate 参数
     */
    @Override
    public void edit(BannerSettingUpdateValidate updateValidate) {
        BannerSetting model = bannerSettingMapper.selectOne(
                new QueryWrapper<BannerSetting>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setUrl(updateValidate.getUrl());
        model.setRemark(updateValidate.getRemark());
        model.setType(updateValidate.getType());
        model.setBusinessType(updateValidate.getBusinessType());
        model.setBusinessId(updateValidate.getBusinessId());
        model.setRequestUrl(updateValidate.getRequestUrl());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        bannerSettingMapper.updateById(model);
    }

    /**
     * 平台banner设置删除
     *
     * @author DEREK
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        BannerSetting model = bannerSettingMapper.selectOne(
                new QueryWrapper<BannerSetting>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        bannerSettingMapper.delete(new QueryWrapper<BannerSetting>().eq("id", id));
    }

}
